<?php

namespace App\Http\Services;

use Illuminate\Support\Facades\DB;

class H5Service
{
    /**
     * 获取用户对应的渠道.
     *
     * @param $reqData
     *
     * @return int|mixed
     */
    public static function getChannel($reqData)
    {
        return $reqData['ch'] ?? 0;
    }

    /**
     * 获取查询的数据库.
     *
     * @param $channel
     *
     * @return \Illuminate\Database\Query\Builder
     */
    private static function getCloneModel($channel)
    {
        if (1 == $channel) {
            return clone DB::table('h5_answer_logs_inside');
        } else {
            return clone DB::table('h5_answer_logs');
        }
    }

    /**
     * 保存答案.
     *
     * @param $client
     * @param $reqData
     *
     * @return array
     */
    public static function saveAnswer($client, $reqData)
    {
        $channel = self::getChannel($reqData);

        //正确答案
        $realAnswer = 'C,B,B,A,AB,AB,B,B,B,B';

        $answer = $reqData['answer'] ?? '';
        $isTrue = $answer === $realAnswer ? 1 : 0;

        $startTimeMicro = $reqData['answer_start'] ?? 0;
        $startTime      = $startTimeMicro ? microtime_to_date_time_str($startTimeMicro) : date_time_str();

        $endTimeMicro   = $reqData['answer_end'] ?? 0;
        $endTime        = $endTimeMicro ? microtime_to_date_time_str($endTimeMicro) : date_time_str();

        $saveData  = array_merge($client, [
            'answer'       => $answer,
            'openid'       => $reqData['openid'] ?? '',
            'is_true'      => $isTrue,
            'answer_start' => $startTime,
            'answer_end'   => $endTime,
            'answer_time'  => ($endTimeMicro - $startTimeMicro) / 1000,
        ]);

        //如果答对且有手机号
        if ($isTrue && !empty($reqData['phone'])) {
            self::getCloneModel($channel)->where('openid', $reqData['openid'])->orderByDesc('id')->update(['phone' => $reqData['phone']]);
        } else {
            self::getCloneModel($channel)->insertGetId($saveData);
        }

        return self::getUserPermission($saveData['openid'], ['ch' => $channel]);
    }

    /**
     * 获取用户权限.
     *
     * @param $openid
     *
     * @return array
     */
    public static function getUserPermission($openid, $reqData = [])
    {
        $channel = self::getChannel($reqData);

        $lastLog    = self::getCloneModel($channel)->where('openid', $openid)->orderByDesc('id')->first();
        $permission = $lastLog ? $lastLog->is_permission : 1;
        if ($lastLog && $lastLog->is_true) {
            $permission = 0;
        }
        $answerCnt = self::getCloneModel($channel)->where('openid', $openid)->count();

        return [
            'is_true'       => $lastLog ? ($lastLog->is_true ? 1 : 0) : 0,
            'is_answered'   => $lastLog ? 1 : 0,
            'is_permission' => (int) $permission,
            'is_has_phone'  => $lastLog ? ($lastLog->phone ? 1 : 0) : 0,
            'is_had_reset'  => $answerCnt > 1 ? 1 : 0,
        ];
    }

    /**
     * 重置答题权限.
     *
     * @param $openid
     *
     * @return array
     */
    public static function resetPermission($openid, $reqData = [])
    {
        $channel = self::getChannel($reqData);

        //最新一条答题记录
        $lastLog      = self::getCloneModel($channel)->where('openid', $openid)->orderByDesc('id')->first();
        $isPermission = $lastLog ? $lastLog->is_permission : 1;
        if ($lastLog && !$lastLog->is_true) {
            //如果已经答错，且只答过一次 则重新设置权限
            if (1 == self::getCloneModel($channel)->where('openid', $openid)->count()) {
                $isPermission = 1;
                self::getCloneModel($channel)->where('openid', $openid)->update(['is_permission' => $isPermission]);
            }
        }

        return ['is_permission' => $isPermission];
    }

    public static function getIsBuyNullList()
    {
        $list = DB::table('sa_game_buy')->where('is_buy', 0)->get(['id', 'game_id', 'is_buy', 'date', 'game_time', 'league', 'team_home', 'team_guest']);
        return $list->toArray();
    }

    public static function saveIsBuy($id)
    {
        throw_if_str(!$id, 'id有误');
        return DB::table('sa_game_buy')->where('id', $id)->update(['is_buy' => 1]);
    }
}
